package com.erebus.view.api;


import com.erebus.base.model.PageResult;
import com.erebus.base.model.RestResponse;
import com.erebus.view.model.dto.ListParams;
import com.erebus.view.model.dto.StudentDto;
import com.erebus.view.model.po.Student;
import com.erebus.view.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    // 查询所有
    @PostMapping("/list")
    public RestResponse list(@RequestBody(required = false) ListParams listParams) {


        PageResult<StudentDto> studentPageResult = studentService.list(listParams);

        return RestResponse.success(studentPageResult, "查询成功");
    }

    // 添加
    @PostMapping("/add")
    public RestResponse add(@RequestBody StudentDto studentDto) {

        int total = studentService.add(studentDto);
        if (total > 0) {
            return RestResponse.success();
        }
        return RestResponse.validfail("添加失败");
    }

    @GetMapping("/selectId/{studentId}")
    public RestResponse selectById(@PathVariable String studentId){

        System.err.println(studentId);


        StudentDto studentDto = studentService.selectById(studentId);

        return RestResponse.success(studentDto);
    }

    // 修改
    @PostMapping("/update")
     public RestResponse<String> update(@RequestBody StudentDto studentDto,   @RequestParam("studentOldId") String studentOldId){
        int i = studentService.update(studentDto,studentOldId);

        if(i > 0){
            return RestResponse.success("修改成功");
        }

        return RestResponse.validfail("修改失败");
    }

    // 删除
    @GetMapping("/delete/{studentId}")
    public RestResponse deleteById(@PathVariable String studentId){
        int i = studentService.deleteById(studentId);

        if(i > 0){
            return RestResponse.success("删除成功");
        }

        return RestResponse.validfail("删除失败");
    }



}
